import { _decorator, Component, Node, Label, Vec3, v3 } from 'cc';
import { BasicUI } from '../../Init/Basic/BasicUI';
import { EventTypes } from '../../Init/Managers/EventTypes';
import { AudioEnum } from '../../Init/SystemAudio/AudioEnum';
import { AudioSystem } from '../../Init/SystemAudio/AudioSystem';
import { StorageSystem } from '../../Init/SystemStorage/StorageSystem';
import { UIEnum } from '../../Init/SystemUI/UIEnum';
import { UISystem } from '../../Init/SystemUI/UISystem';
import { AssetAnimCmp } from './AssetAnimCmp';
import EventManager from '../../Init/Managers/EventManager';
import Tools from '../../Init/Tools/Tools';
const { ccclass, property } = _decorator;

@ccclass('PlayerAssetsUI')
export class PlayerAssetsUI extends BasicUI {

    @property(Label)
    protected assetLabel: Label = null;
    @property(Label)
    protected energyLabel: Label = null;

    @property(AssetAnimCmp)
    protected animCmp: AssetAnimCmp = null;
    @property(Node)
    protected goldAnimTarget: Node = null;
    @property(Node)
    protected energyAnimTarget: Node = null;

    curEnergy = 0;
    curAsset = 0;
    curFood = 0;

    protected onEvents() {
        this.on(EventTypes.GameEvents.UserAssetsChanged, this.onUserAssetsChanged, this);
    }

    public show(d?) {
        super.show(d);

        this.setAssetInfo();
    }
    public hide(d?: any): void {
        super.hide(d);
    }

    // #region ----------------私有方法---------
    /**显示资源数据 */
    private setAssetInfo() {
        let userAssets = StorageSystem.getData().userAssets;
        this.curAsset = userAssets.asset;
        this.curEnergy = userAssets.energy;

        this.assetLabel.string = Tools.getFormatStr(userAssets.asset);
        this.energyLabel.string = Tools.getFormatStr(userAssets.energy);
    }
    // #endregion

    // #region -----------------事件-------------------
    private tmpP = v3();
    /**资产变更后 UI同步显示 */
    protected onUserAssetsChanged(isAnim = false, isMask = true) {
        if (isAnim) {
            let userAssets = StorageSystem.getData().userAssets;
            let maskCount = 0;
            const finishCb = () => {
                maskCount--;
                if (maskCount <= 0) {
                    EventManager.emit(EventTypes.UIEvents.HideTouchMask);
                }
            }
            if (isMask) {
                EventManager.emit(EventTypes.UIEvents.ShowTouchMask);
            }
            // 金币动画
            if (userAssets.asset > this.curAsset) {
                isMask && (maskCount++);
                this.curAsset = userAssets.asset;
                this.tmpP.set(this.goldAnimTarget.position).add(this.goldAnimTarget.parent.position);
                this.animCmp.showAnim(Vec3.ZERO, this.tmpP, 'assetItem', () => {
                    this.setAssetInfo();
                    isMask && finishCb();
                }, isMask)
            }
            // 体力动画
            if (userAssets.energy > this.curEnergy) {
                isMask && (maskCount++);
                this.curEnergy = userAssets.energy;
                this.tmpP.set(this.energyAnimTarget.position).add(this.energyAnimTarget.parent.position);
                this.animCmp.showAnim(Vec3.ZERO, this.tmpP, 'energyItem', () => {
                    this.setAssetInfo();
                    isMask && finishCb();
                }, isMask)
            }
        } else {
            this.setAssetInfo();
        }
    }

    // #endregion

    //#region -----------------按钮----------------
    protected onAddGoldBtnClick() {

        UISystem.hideUI(UIEnum.SupplyUI);
        UISystem.showUI(UIEnum.SupplyUI, { isGoldPanel: true, isAutoHide: false });
    }
    protected onAddEnergyBtnClick() {
        UISystem.hideUI(UIEnum.SupplyUI);
        UISystem.showUI(UIEnum.SupplyUI, { isGoldPanel: false, isAutoHide: false });
    }

    //#endregion
}

